import {base, toast, promptBox} from 'nc-lightapp-front';
import refs from "./refs";

export default class CopyAction {
    constructor(comp) {
        this.comp = comp;
    }

    copy = async (selections, fool, callback) => {
        const {props, action} = this.comp;
        const {dispatch, main} = props;
        const {orgValue, language} = main;
        const pk_orgs = [];
        selections.forEach(item => {
            if (item.refpk !== orgValue.refpk) {
                pk_orgs.push(item.refpk)
            }
        });
        if (!pk_orgs || pk_orgs.length === 0) {
            toast({
                color: 'error',
                content: language['gx6008-000291']
            });
            return;
        }
        let res = await dispatch({
            type: 'main/copy',
            payload: {
                pk_org: orgValue.refpk,
                pk_orgs: pk_orgs,
                opFlag: "1"
            }
        });
        if (res.success) {
            if (res.data.msg) {
                refs.alertModalNode.show(res.data.msg, (opFlag) => {
                    this.confirmCopy(opFlag, selections, callback)
                })
            } else {
                toast({
                    color: 'success',
                    content: language['gx6008-000290']
                });
                callback && callback();
            }
        }
    };

    confirmCopy = async (opFlag, selections, callback) => {
        const {props, action} = this.comp;
        const {dispatch, main} = props;
        const {orgValue, language} = main;
        let res = await dispatch({
            type: 'main/copy',
            payload: {
                pk_org: orgValue.refpk,
                pk_orgs: selections.map(item => item.refpk),
                opFlag: opFlag
            }
        });
        if (res.success) {
            toast({
                color: 'success',
                content: language['gx6008-000290']
            });
            callback && callback();
        }
    }
}
